package com.wiegand.loader.loaderdisplay {
	import flash.text.TextFieldAutoSize;
	import com.caurina.transitions.Tweener;
	import com.wiegand.loader.ILoaderDisplay;
	import com.wiegand.util.ColorRectangle;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 * @author kwiegand
	 */
	public class MainLoader extends Sprite implements ILoaderDisplay{
		
		public static var EVENT_LOADER_FINISHED: String = "loader finished";
		public static var EVENT_LOADER_CLOSED: String = "loader closed";
		
		private static var _instance : 	MainLoader;

		private var _background : Sprite;
		private var _line : Sprite;
		private var _percent : Number;
		private var _wall : Sprite;
		private var _floor : Sprite;
		private var _mask : Sprite;
		private var _tf : TextField;
		private var _format : TextFormat;

		public static function getInstance() : MainLoader{
			
			if( _instance == null )
				_instance = new MainLoader( new SingletonEnforecer() );
			return _instance;
		}
		
		public function MainLoader( blocker:SingletonEnforecer ):void{
			if ( blocker == null ) {
            	throw new Error("Error: Instantiation failed: Use MainLoader.getInstance() instead of new.");
          	}
          	else{
          		init();
          	}
 		}
 		
 		private function init():void{
		}
		
 		public function initialize():void{	
 			_background = ColorRectangle.create(0x000000,1,stage.stageWidth,stage.stageHeight);
 			_background.alpha = 0; 			
 			addChild(_background);
 			
 			_mask = new Sprite();
 			addChild(_mask);
 			_wall = ColorRectangle.create(0xff0000,1,stage.stageWidth,Math.round(stage.stageHeight*0.75));
			_mask.addChild(_wall);
			_floor = ColorRectangle.create(0xff0000,1,stage.stageWidth,Math.round(stage.stageHeight*0.5));
			_floor.y = stage.stageHeight*0.75;
 			_mask.addChild(_floor);
 			
 			_background.mask = _mask;
 			
 			_line = ColorRectangle.create(0xffffff,1,1,1);
 			_line.alpha = 0; 			
 			_line.y = Math.round(stage.stageHeight*0.75);
 			addChild(_line);
 			
 			_format = new TextFormat("Verdana", 8, 0xffffff);
 			_tf = new TextField();
 			_tf.autoSize = TextFieldAutoSize.LEFT;
 			_tf.embedFonts=true;
			_tf.text=" ";
 			_tf.setTextFormat(_format);
 			addChild(_tf);
		}
		
		public function open() : void {
			Tweener.addTween(_background,{ alpha:1, delay:.5, time:1, transition:"linear"} );
		}

		public function close() : void {
			dispatchEvent(new Event(MainLoader.EVENT_LOADER_FINISHED));
			Tweener.removeTweens(_background);
			Tweener.removeTweens(_line);
			Tweener.removeTweens(_wall);
			Tweener.removeTweens(_floor);
			Tweener.addTween(_wall,{ height:0.1, delay:2, time:3, transition:"easeoutexpo", onComplete:callClosed} );
			Tweener.addTween(_floor,{ height:0.1, y:stage.stageHeight, delay:2, time:3, transition:"easeoutexpo"} );
			Tweener.addTween(_line,{ alpha:0, time:1, delay:.7, transition:"linear"} );
			Tweener.addTween(_tf,{ alpha:0, time:1, delay:.5, transition:"linear"} );
		}

		public function callClosed() : void {
			dispatchEvent(new Event(MainLoader.EVENT_LOADER_CLOSED));
		}

		public function clear() : void {
			Tweener.removeTweens(_background);
			Tweener.removeTweens(_line);
			Tweener.removeTweens(_floor);
			Tweener.removeTweens(_wall);
			Tweener.removeTweens(_tf);
			removeChild(_background);
			removeChild(_line);
			removeChild(_mask);
			removeChild(_tf);
		}
		
		public function update(percent : Number) : void {
			_line.alpha = 1; 
			_percent = percent;
			tween();
		}
		
		private function tween():void{	
			Tweener.removeTweens(_line);
			var foo : int = Math.round((_percent*stage.stageWidth)/100);			
			Tweener.addTween(_line,{ width:foo, time:5, transition:"easeinoutexpo", onUpdate:updateTf, onComplete:checkPercent} );
		}
		
		private function updateTf():void{
			_tf.text = Math.round((_line.width*100)/stage.stageWidth) +"% ";
			_tf.setTextFormat(_format);
			
			_tf.x=Math.round(_line.x+_line.width-_tf.textWidth);
 			_tf.y=Math.round(_line.y+_tf.textHeight/2-3);
		}
		
		private function checkPercent():void{
			if(_percent>=100) close();
			if(_percent>=50) Tweener.addTween(_background,{ alpha:.65, delay:0, time:.5, transition:"linear"} );
			
		}
		
		public function setPosition(x : Number, y : Number) : void {
		}
		
		public function resize():void{
			_wall.width = stage.stageWidth;
			_wall.height = stage.stageHeight*0.75;
			_floor.width = stage.stageWidth;
			_floor.height = stage.stageHeight*0.5;
			_floor.y = stage.stageHeight*0.75;
			
			_background.width = stage.stageWidth;
			_background.height = stage.stageHeight;
			_line.y = Math.round(stage.stageHeight*0.75);
 			
			tween();
		}
	}
}
internal class SingletonEnforecer {}
